package service;

import dao.DB;
import entity.UserLogin;

import java.util.List;
import java.util.Map;

public class UserService {


//    登录验证
    UserLogin user =new UserLogin();
    public UserLogin login(String acc , String pwd){
        List<Map<String, Object>> result = DB.executeQuery("select * from user where account = ?", acc);
        String databasePwd = null;

        // 取出数据库里的密码
        for (int i = 0; i < result.size(); i++) {
            user.setId((int) result.get(i).get("id"));
            user.setAccount((String) result.get(i).get("account"));
            user.setTOKEN((String) result.get(i).get("TOKEN"));
            databasePwd = (String) result.get(i).get("password");
        }
        // 如果数据库的密码和传入的密码相同，登录成功
        if (pwd.equals(databasePwd)) {
            return user;
        }
        return null;
    }

    public UserLogin register(UserLogin user) {
        // 根据传入的用户对象，插入到数据库里
        int r = DB.executeUpdate(" INSERT INTO `user` (`id`, `account`, `password`) VALUES (null, ?, ?);" ,user.getAccount(),user.getPassword());
        // 返回值为-1，表示插入失败，其他表示成功
        if (r == -1) {
            // 插入失败就返回null
            return null;
        }
        // 插入成功返回插入的对象
        return user;
    }

//设置token
    public void setToken(Integer id, String token) {
        DB.executeUpdate("update user set TOKEN = ? where id = ?", token, id);

    }
//查询好友列表
    public List getfriendCuont(String token){
        List fcuont =DB.executeQuery("SELECT * FROM friend f LEFT JOIN `user` u on f.friendid = u.account " +
                "WHERE f.userid = (SELECT account FROM `user` WHERE TOKEN =? ) and f.friStatus =1 " , token);
//        List fcuont =DB.executeQuery("SELECT * FROM `user` WHERE TOKEN = ? " , token);
        return fcuont;

    }

//添加好友查找好友
    public List getAddfriend(String addFri) {
        List  FriendAcc = DB.executeQuery("SELECT * FROM user WHERE account = ?" ,addFri);
        return FriendAcc;
    }
//添加好友
    public void addFriend(String token,String fid){
        String uid = (String) DB.executeQuery("SELECT account FROM `user` WHERE token = ?" ,token).get(0).get("account");
        DB.executeUpdate("INSERT INTO `friend`( `userid`, `friendid`,  `friStatus`) " +
                "VALUES ( "+uid+", "+fid+",  "+false+");");

    }
    //查找好友信息
    public List getPagefriinfo(String userid ,String friacc) {
        List resp = DB.executeQuery("SELECT * FROM `user` u LEFT JOIN friend f on" +
                "  u.account =f.friendid WHERE f.userid ="+userid+" and f.friendid="+friacc+"");

        return resp;
    }
//    查询该用户信息
    public List<Map<String,Object>> getUsermeinfo(String token){
        List<Map<String,Object>> resp = DB.executeQuery("SELECT * FROM `user` WHERE TOKEN = ?",token);
        return resp;
    }

    //我发送的消息
    public List getPagemeotes(String uid ,String fid){
       List  resp = DB.executeQuery("SELECT * FROM pagenotes WHERE Tuid = ? and Tfid = ? or Tuid = ? and Tfid = ?  ORDER BY taketime",uid,fid,fid,uid);
       return  resp;
    }


}